package com.vchello.service.impl.security;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.vchello.bean.security.UserRole;
import com.vchello.bean.user.User;
import com.vchello.dao.security.UserRoleDao;
import com.vchello.service.security.UserRoleService;

/**
 * 
 * @author power
 *
 */
@Service(value = "userRoleService")
public class UserRoleServiceImpl extends ServiceImpl<UserRoleDao, UserRole> implements UserRoleService {
	
	@Autowired
	private UserRoleDao userRoleDao;

	@Override
	public UserRole findUserRole(int userId) {
		Wrapper<UserRole> wrapper =  new EntityWrapper<UserRole>();
		wrapper.eq("userId", userId);
		return userRoleDao.selectList(wrapper).get(0);
	}

	@Override
	public boolean updateUserRole(UserRole userRole) {
		return userRoleDao.updateById(userRole)>0;
	}

	@Override
	public Page<UserRole> findUserRoleByFatherId(Page<UserRole> page, User user, int roleId) {
		Wrapper<UserRole> wrapper = new EntityWrapper<UserRole>();
		wrapper.eq("user_id", user.getId()).eq("role_id", roleId);
		return selectPage(page, wrapper);
	}

	@Override
	public int findUserRoleChildren(int userId) {
		return userRoleDao.findUserRoleChildren(userId);
	}

	@Override
	public List<UserRole> findUsreRoleByFatherId(int fatherId) {
		return userRoleDao.findUsreRoleByFatherId(fatherId);
	}

	@Override
	public boolean deleteUserRole(UserRole userRole) {
		try {
			userRoleDao.deleteById(userRole);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean saveUserRole(UserRole userRole) {
		return userRoleDao.insert(userRole)>0;
	}

	@Override
	public List<UserRole> findUserRoleByRoleId(int userId, int roleId) {
		return userRoleDao.findUserRoleByRoleId(userId, roleId);
	}
}
